#Alexander F. Gazmararian
#afg2@princeton.edu

library(tidyverse)
library(RankAggreg)

source("code/fun/book_theme.r")
source("code/fun/savefig.r")

#load data
g <- readRDS("data/SWPA_Youth_Career.rds")

#create table of marginal frequencies
with(g, table(career, rank))

rank.plot <-
  g %>%
  dplyr::mutate(
    career = dplyr::case_when(
      career == "hvac" ~ "Energy efficiency",
      career == "masonry" ~ "Masonry",
      career == "diesel" ~ "Diesel mechanics",
      career == "construct" ~ "Construction"
    )
  ) %>%
  dplyr::group_by(rank, career) %>%
  dplyr::count() %>%
  dplyr::group_by(career) %>%
  dplyr::mutate(pct=prop.table(n),
                career = factor(
                  career,
                  ordered = TRUE,
                  levels = c(
                    "Masonry", "Construction", "Diesel mechanics", "Energy efficiency"
                  )
                )) %>%
  ggplot(aes(x = rank,y=pct,label=scales::percent(pct,accuracy=1),fill=career)) +
  geom_col() +
  geom_text(vjust=-.5,size=3) +
  scale_fill_grey() +
  scale_y_continuous(labels = scales::percent, expand = c(0,0),limits=c(0,.35)) +
  labs(x = "Rank", y = "", fill = "") +
  facet_wrap(~career,ncol=4) +
  book_theme +
  theme(legend.position = "")

rank.plot

savefig(rank.plot, "7.2_figure_swpayouth", height = 2, filepath = "figures/")

#Weighted rank aggregation----
g <- g[order(g$rank), ]
g_mat <- g %>% tidyr::pivot_wider(id_cols = id, names_from = rank, values_from = career)
g_mat <- as.matrix(g_mat[, -1])
#estimate optimal list
optlist <- BruteAggreg(g_mat, 4)
optlist
# Save figure
png("figures/ch7/si_optlist_swpayouth.png", width = 6.5, height = 7, res = 300, units = "in", pointsize = 10)
plot(optlist)
dev.off()
